package com.gamota.mobi.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.GridView;

public class AutoGrowGridView extends GridView implements OnScrollListener {
	
	private static final String TAG = "AutoGrowGridView";
	private OnScrollListener mOnScrollListener;
	private OnLoadMoreListener mOnLoadMoreListener;
	private boolean mIsLoadingMore = false;
	private int mCurrentScrollState;
	//private ILoadMoreListener listener;
	//private LayoutInflater mInflater;
	//private RelativeLayout mFooterView;
	//private ProgressBar mProgressBarLoadMore;

	public AutoGrowGridView(Context context) {
		super(context);
		init(context);
	}
	
	public AutoGrowGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public AutoGrowGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context) {
//		if(null == mFooterView){
//			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//			mFooterView = (RelativeLayout) mInflater.inflate(R.layout.load_more_footer, this, false);
//			mProgressBarLoadMore = (ProgressBar) mFooterView.findViewById(R.id.load_more_progressBar);
//		} else {
//			mFooterView = getmFooterView();
//		}
		super.setOnScrollListener(this);
	}
	
//	public void setLoadMoreListener(ILoadMoreListener l){
//		this.listener = l;
//	}

	@Override
	public void setOnScrollListener(OnScrollListener l) {
		this.mOnScrollListener = l;
	}
	
	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (mOnScrollListener != null) {
			mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
		}
//		if(mOnLoadMoreListener != null){
//			if (visibleItemCount == totalItemCount) {
//				if(listener != null){
//					listener.onLoadComplete();
//				}
//				return;
//			}
//		}
//		boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
//		if (!mIsLoadingMore && loadMore && mCurrentScrollState != SCROLL_STATE_IDLE) {
//			if(listener != null){
//				listener.onLoading();
//			}
//			mIsLoadingMore = true;
//			onLoadMore();
//		}
	}
	
	public void onLoadMore() {
		Log.d(TAG, "onLoadMore");
		if (mOnLoadMoreListener != null) {
			mOnLoadMoreListener.onLoadMore();
		}
	}
	
//	public void onLoadMoreComplete() {
//		if(listener != null){
//			listener.onLoadComplete();
//		}
//		mIsLoadingMore = false;
//	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		mCurrentScrollState = scrollState;
		if (mOnScrollListener != null) {
			mOnScrollListener.onScrollStateChanged(view, scrollState);
		}
	}
	
	public interface OnLoadMoreListener {
		/**
		 * Called when the list reaches the last item (the last item is visible
		 * to the user)
		 */
		public void onLoadMore();
	}

}
